package com.jhx.mall.dao.impl;

import com.jhx.mall.dao.BaseDao;
import com.jhx.mall.dao.DtsCartDao;
import com.jhx.mall.entity.DtsBrand;
import com.jhx.mall.entity.DtsCart;
import com.jhx.mall.entity.DtsCategory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author 李琪
 * @Date 2025/4/1 15:27
 **/
public class DtsCartDaoImpl extends BaseDao implements DtsCartDao {
    @Override
    public List<DtsCart> selectCartByUserId(Long userId) {
        List<DtsCart> list=new ArrayList<>();
        String sql="select  a.id,b.`name`,a.price,a.number,a.pic_url,a.specifications from dts_cart a inner join dts_brand b on a.brand_id=b.id where a.deleted=0 and a.user_id=?";
        rs=executeQuery(sql,userId);
        try {
            while (rs.next()) {
                DtsCart dtsCart = new DtsCart();
                dtsCart.setId(rs.getLong("id"));
                dtsCart.setPrice(rs.getDouble("price"));
                dtsCart.setNumber(rs.getLong("number"));
                dtsCart.setPicUrl(rs.getString("pic_url"));
                dtsCart.setSpecifications(rs.getString("specifications"));

                DtsBrand dtsBrand=new DtsBrand();
                dtsBrand.setName(rs.getString("name"));
                dtsCart.setDtsBrand(dtsBrand);
                list.add(dtsCart);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            super.closeAll(rs,pstmt,conn);
        }
        return list;
    }

    @Override
    public int deleteCartByUserId(Long Id) {
        String sql="update dts_cart set deleted=1 where id=?";
        return super.executeUpdate(sql,Id);
    }

    @Override
    public int updateCartByUserId(Long Id, Long number) {
        String sql="update dts_cart set number=? where id=?";
        return super.executeUpdate(sql,number,Id);
    }
}
